package com.ysd.lis.service.self.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.callNum.CallWindow;
import com.ysd.lis.entity.eq.EqEquipmentImage;
import com.ysd.lis.entity.slef.SelfMachine;
import com.ysd.lis.mapper.self.SelfMachineMapper;
import com.ysd.lis.request.FeeItemPageParam;
import com.ysd.lis.service.self.SelfMachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-05-12
 */
@Service
public class SelfMachineServiceImpl extends ServiceImpl<SelfMachineMapper, SelfMachine> implements SelfMachineService {

    @Autowired
    SelfMachineMapper machineMapper;
    @Override
    public Result findMachineList(FeeItemPageParam feeItemPageParam) {

        LambdaQueryWrapper<SelfMachine> queryWrapper=new LambdaQueryWrapper<>();

        String keywords = feeItemPageParam.getKeyWords();
        if (StrUtil.isNotEmpty(keywords)) {
            queryWrapper.and(wrapper -> wrapper.like(SelfMachine::getMachineCode, keywords).or().like(SelfMachine::getMachineName, keywords));
        }
        queryWrapper.eq(SelfMachine::getDelFlag,0);

        List<SelfMachine> selfMachineTypes = machineMapper.selectList(queryWrapper);
        return Result.succ(selfMachineTypes);
    }

    @Override
    public Result savaOrUpdateMachine(SelfMachine machine) {
        LambdaQueryWrapper<SelfMachine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SelfMachine::getSuid,machine.getSuid())
                .eq(SelfMachine::getDelFlag,0);

        boolean b = this.saveOrUpdate(machine,queryWrapper);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result deleteMachine(List<SelfMachine> machineList) {
        List<String> ids = machineList.stream().map(SelfMachine::getId).filter(StringUtils::isNotBlank).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败");
        }    }

    @Override
    public Result uploadSelfImage(String id, Map<String, String> map) {

        SelfMachine selfMachine = new SelfMachine();
        selfMachine.setId(id);
        selfMachine.setImagePath(map.get("path"));
        selfMachine.setImageName(map.get("name"));
        this.updateById(selfMachine);
        return Result.succ(1, "上传成功", selfMachine);
    }

    @Override
    public Result findMachineBySuid(String suid) {
        if (StrUtil.isEmpty(suid)){
            return Result.fail("suid为空");
        }
        LambdaQueryWrapper<SelfMachine> queryWrapper =  new LambdaQueryWrapper<>();
        queryWrapper.eq(SelfMachine::getSuid,suid)
                .eq(SelfMachine::getDelFlag,0);
        SelfMachine selfMachine = machineMapper.selectOne(queryWrapper);

        // 必须做null判断
        if (selfMachine == null) {
            // 处理不存在的情况
            return Result.fail("未找到该设备");
        }else {
            return Result.succ(selfMachine);
        }

    }

    @Override
    public Result findMachines(FeeItemPageParam feeItemPageParam) {
        Map<String,Object> returnMap = new HashMap<>();

        List<SelfMachine> windowList = machineMapper
                .selectList(new LambdaQueryWrapper<SelfMachine>().eq(SelfMachine::getDelFlag, 0));
        returnMap.put("allCnt",windowList.size());
        //查询未启用的数量
        Integer noWindowCnt = Math.toIntExact(windowList.stream().filter(a -> !"1".equals(a.getStatus())).count());
        returnMap.put("runCnt",noWindowCnt);


        return Result.succ(1, "查询成功", returnMap);
    }
}
